import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-06-18
 * Time: 21:43
 */
class Student {
    public int id;
    public String name;
    public String subject;
    public int score;
}
class Book {
    public String name;
    public String author;
    public String publishingHouse;
    public int id;
    public double price;


    public Book(String name, String author, String publishingHouse, int id, double price) {
        this.name = name;
        this.author = author;
        this.publishingHouse = publishingHouse;
        this.id = id;
        this.price = price;
    }

    @Override
    public String toString() {
        return name + ' ' + author + ' ' + publishingHouse + ' ' + id + ' ' + price;
    }
}
class Library {
    public List<Book> bookList = new ArrayList<>();
    public void addBook(Book b) {
        bookList.add(b);
    }
    public void persist() {
        FileWriter fileWriter = null;
        BufferedWriter bufferedWriter = null;
        try {
            File file = new File("books.dat");
            fileWriter = new FileWriter(file);
            bufferedWriter = new BufferedWriter(fileWriter);
            for (Book book :
                    bookList) {
                bufferedWriter.write(book.toString());
                bufferedWriter.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedWriter != null)
                    bufferedWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public Book[] restore() {
        Book[] books = new Book[bookList.size()];
        int i = 0;
        BufferedReader bufferedReader = null;
        try {
            File file = new File("books.dat");
            FileReader fileReader = new FileReader(file);
            bufferedReader = new BufferedReader(fileReader);
            String str;
            while ((str = bufferedReader.readLine()) != null) {
                String[] strings = str.split(" ");
                books[i++] = new Book(strings[0], strings[1], strings[2], Integer.parseInt(strings[3]), Double.parseDouble(strings[4]));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader != null)
                    bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return books;
    }
}
public class Main {
    public static void main(String[] args) {
        Library library = new Library();
        library.addBook(new Book("平凡的生活", "路遥", "人民出版社", 12, 36.5));
        library.addBook(new Book("数据结构与算法分析", "Mark", "机械工业出版社", 11, 55.1));
        library.addBook(new Book("Java核心技术", "霍斯特曼", "机械工业出版社", 3, 41.0));
        library.persist();
        Book[] books = library.restore();
        for (Book b :
                books) {
            System.out.println(b);
        }
    }
    public static void main1(String[] args) {
        BufferedReader bufferedReader = null;
        BufferedReader bufferedReader1 = null;
        List<String> list = null;
        List<String> list1 = null;
        try {
            FileReader fileReader = new FileReader(new File("dishes.txt"));
            FileReader fileReader1 = new FileReader(new File("orders.txt"));
            bufferedReader = new BufferedReader(fileReader);
            bufferedReader1 = new BufferedReader(fileReader1);
            list = new ArrayList<>();
            list1 = new ArrayList<>();
            String s = null;
            while ((s = bufferedReader.readLine()) != null) {
                list.add(s);
            }
            while ((s = bufferedReader1.readLine()) != null) {
                list1.add(s);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (bufferedReader != null)
                    bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bufferedReader1 != null)
                    bufferedReader1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Map<String, Integer> mapDishes = new HashMap<>();
        Map<String, Integer> mapOrders = new HashMap<>();
        for (String str :
                list) {
            String[] strings = str.split(":");
            mapDishes.put(strings[0], Integer.parseInt(strings[1]));
        }
        for (String str :
                list1) {
            String[] strings = str.split(":");
            mapOrders.put(strings[0], Integer.parseInt(strings[1]));
        }
        int total = 0;
        for (Map.Entry<String, Integer> entry :
                mapOrders.entrySet()) {
            String str = entry.getKey();
            int count = entry.getValue();
            total += mapDishes.get(str) * count;
        }
        System.out.println(total);
    }
}
